package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    //查询用户
    @Override
    public List<User> findAll() {
        Connection conn =null;
        Statement stat =null;
        ResultSet res =null;
        ArrayList<User> list=new ArrayList<>();
        try {
            conn = JDBCUtils.getConnection();
            stat = conn.createStatement();
            String sql="select * from user";
            res = stat.executeQuery(sql);
            while (res.next()){
                int id = res.getInt("id");
                String name = res.getString("name");
                String gender = res.getString("gender");
                int age = res.getInt("age");
                String address = res.getString("address");
                String qq = res.getString("qq");
                String email = res.getString("email");
                User user=new User(id,name,gender,age,address,qq,email);
                list.add(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res,stat,conn);
        }
        return list;
    }
//添加用户
    @Override
    public void addUser(User user) {
        Connection conn =null;
        Statement stat =null;
        ArrayList<User> list=new ArrayList<>();
        try {
            conn = JDBCUtils.getConnection();
            stat = conn.createStatement();
            String sql="insert into user values(null,'"+user.getName()+"','"+user.getGender()+"',"+user.getAge()+",'"+user.getAddress()+"','"+user.getQq()+"','"+user.getEmail()+"')";
            stat.executeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,stat,conn);
        }

    }
//回显数据
    @Override
    public User findById(String uid) {
        Connection conn =null;
        Statement stat =null;
        ResultSet res=null;
        User user=null;
        try {
            conn = JDBCUtils.getConnection();
            stat = conn.createStatement();
            String sql="SELECT * FROM USER WHERE id="+uid;
            res = stat.executeQuery(sql);
            if(res.next()){
                int id = res.getInt("id");
                String name = res.getString("name");
                String gender = res.getString("gender");
                int age = res.getInt("age");
                String address = res.getString("address");
                String qq = res.getString("qq");
                String email = res.getString("email");

                user=new User(id,name,gender,age,address,qq,email);

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res,stat,conn);
        }
        return user;
    }

    @Override
    public void updateUser(User user) {
        Connection conn =null;
        Statement stat =null;

        try {
            conn = JDBCUtils.getConnection();
            stat = conn.createStatement();
            String sql = "update user set name = '" + user.getName() + "',gender = '" +
                    user.getGender() + "' ,age = " + user.getAge() + " , address = '" + user.getAddress() + "' ," +
                    " qq = '" + user.getQq() + "', email = '" + user.getEmail() + "' where id = " + user.getId();

            stat.executeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,stat,conn);
        }
    }

    @Override
    public void deleteUser(String id) {
        Connection conn =null;
        Statement stat =null;

        try {
            conn = JDBCUtils.getConnection();
            stat = conn.createStatement();
            String sql ="DELETE FROM USER WHERE id="+id;

            stat.executeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,stat,conn);
        }
    }
}
